//
//  WLTabBar.swift
//  WLWeibo
//
//  Created by Devil on 16/8/11.
//  Copyright © 2016年 Wiliam. All rights reserved.
//

import UIKit

protocol WLTabBarDelegate:NSObjectProtocol {
    //声明方法
    func tabBarButtonClick()
}


class WLTabBar: UITabBar {
    //声明代理
    weak var homeDelegate:WLTabBarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //监听
    func buttonClick(){
        
        homeDelegate?.tabBarButtonClick()
        
    }
    //设置视图
    private func setupUI(){
        addSubview(composeButton)
    }
    //给自控制器设置frame
    override func layoutSubviews() {
        
        super.layoutSubviews()
        //子空间宽度
        let childW = self.frame.width * 0.2
        var index:CGFloat = 0
        //遍历子控件
        for value in self.subviews {
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.size.width = childW
                value.frame.origin.x = index * childW
                
                index++
                
                if index == 2{
                    
                    index++
                    
                }
                
            }
        }
        
        composeButton.center.x = self.frame.width * 0.5
        composeButton.center.y = self.frame.height * 0.5
        
        
    }
    
    private lazy var composeButton: UIButton = {
        
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        //设置背景图片
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        //设置图片
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        //自适应
        button.sizeToFit()
        return button
    }()
    
}
